from DBA.dbmanager import DBInterface
from AccessPointDAC import AccessPoint

class Salle:
    """
    Classe d acces aux salles referencees dans la base
    Table: PR5QTSA
    """   
    def __init__(self,id=""):
        """
        Constructeur
        @param id identificateur de la salle: soit on le rentre, soit il sera genere automatiquement.
        """
        self.id            = {"key":"ID","value":id}
        self.name        = {"key":"nom","value":""}
        #self.description    = {"key":"description","value":""}
        self.xposition        = {"key":"x","value":""}
        self.yposition        = {"key":"y","value":""}
        self.zposition        = {"key":"z","value":""}
        self.height        = {"key":"realheight","value":""}
        self.width        = {"key":"realwidth","value":""}
        self.urlPlan        = {"key":"url_plan","value":""}
        self.ensAccessPoint    = []
        self.__interfacebase    = DBInterface()
        if not id:
            self.id["value"]    = str(self.__interfacebase.createNewID());
    
    def createByDico(self,dico):
        """
        Creation de l'objet depuis un dictionnaire genere en page3.py
        @param dico dictionnaire de salle
        """
        self.name["value"]     = dico[self.name["key"]]
        self.height["value"]     = dico[self.height["key"]]
        self.xposition["value"] = dico[self.xposition["key"]]
        self.yposition["value"] = dico[self.yposition["key"]]
        self.zposition["value"] = dico[self.zposition["key"]]
        self.width["value"]     = dico[self.width["key"]]
        self.urlPlan["value"]     = dico[self.urlPlan["key"]]
    
    
    def addAccessPoint(self,AccessPoint):
        """
        Ajout d'un AP a une salle
        @param AccessPoint objet de type AccessPoint()
        """
        self.ensAccessPoint.append(AccessPoint)
    
    def nbAccessPoint(self):
        """
        Retourne le nombre d'access point pour la salle courante
        """
        return self.ensAccessPoint.count()
    
    def selectAccessPointFromDB(self):
        """
        Retourne la liste des access point pour la salle courante
        """
        key ={}
        key["link_sa"] = self.id["value"]
        self.ensAccessPoint = self.__interfacebase.selectTable(self.__interfacebase.accessPointtablename,self.__interfacebase.accessPointmapping,key)
        return self.ensAccessPoint

    def insertToDb(self,idBatiment):
        """
        Inserer la salle courante dans la BDD.
        @param idBatiment identificateur unique de batiment dans lequel se trouve la salle
        """
        dico = self.createSalleDico(idBatiment)
        self.__interfacebase.insert(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,dico)
        for AccessPoint in self.ensAccessPoint:
            AccessPoint.insertToDb(self.id["value"])

    def deleteFromDb(self):
        """
        Suppression de la salle courante de la BDD.
        """
        self.selectAccessPointFromDB()
        #On initialise au cas ou il n'y aurai pas d' accesspoint
        elementsuppr= 0
        for Accesspoint in self.ensAccessPoint:
            ap = AccessPoint(Accesspoint['ID'])
            elementsuppr=elementsuppr+int(ap.deleteFromDb(self.id["value"]))
        key ={}
        key["ID"] = self.id["value"]
        elementsuppr = elementsuppr + int(self.__interfacebase.deleteFromTable(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,key))
        return elementsuppr

    def updateIntoDb(self,idBatiment):
        """
        Mise a jour des informations et des AP d'une salle
        @param idBatiment identificateur unique de batiment dans lequel se trouve la salle
        """
        dico = self.createSalleDico(idBatiment)
        self.__interfacebase.update(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,dico)
        for AccessPoint in self.ensAccessPoint:
            AccessPoint.updateIntoDb(self.id["value"])

    def createSalleDico(self,idBatiment):
        """
        Retourne un dictionnaire contenant les informations de la salle en cours
        @param idBatiment identificateur unique de batiment dans lequel se trouve la salle
        """
        dico ={}
        dico[self.id["key"]]        = self.id["value"]
        dico["link_ba"]            = idBatiment
        dico[self.name["key"]]        = self.name["value"]
        #dico[self.description["key"]]    = self.description["value"]
        dico[self.xposition["key"]]    = self.xposition["value"]
        dico[self.yposition["key"]]    = self.yposition["value"]
        dico[self.zposition["key"]]    = self.zposition["value"]
        dico[self.height["key"]]    = self.height["value"]
        dico[self.width["key"]]        = self.width["value"]
        dico[self.urlPlan["key"]]    = self.urlPlan["value"]
        return dico

if __name__=="__main__":
    bat = Salle()    
    bat.name["value"]        = "test";
    #bat.description["value"]    = "t";
    bat.xposition["value"]        = "tz";
    bat.yposition["value"]        = "ta";
    bat.zposition["value"]        = "te";
    bat.height["value"]        = "tq";
    bat.width["value"]        = "t";
    
    print bat
